package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.model.Clazz;
import com.model.Paper;
import com.model.User;
import com.service.PaperService;
import com.util.BaseAction;
import com.util.Pagination;
import com.util.QueryTerm;

public class PaperAction extends BaseAction {

    public Set getDataset() {
        return dataset;
    }

    public void setDataset(Set dataset) {
        this.dataset = dataset;
    }

    public List getClazzList() {
        return clazzList;
    }

    public void setClazzList(List clazzList) {
        this.clazzList = clazzList;
    }

    public void setPaperId(int paperId) {
        this.paperId = paperId;
    }

    public void setSelectOne(String selectOne) {
        this.selectOne = selectOne;
    }

    public void setTeacherOrexam(String teacherOrexam) {
        this.teacherOrexam = teacherOrexam;
    }

    public void setIsjava(String isjava) {
        this.isjava = isjava;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    private static final long serialVersionUID = -2405349264584254229L;

    public Pagination getPaginationjson() {
        return paginationjson;
    }

    public void setPaginationjson(Pagination paginationjson) {
        this.paginationjson = paginationjson;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<?> getDatalist() {
        return datalist;
    }

    public void setDatalist(List<?> datalist) {
        this.datalist = datalist;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public int getCurrentpage() {
        return currentpage;
    }

    public void setCurrentpage(int currentpage) {
        this.currentpage = currentpage;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public PaperService getPaperService() {
        return PaperService;
    }

    public void setPaperService(PaperService paperService) {
        PaperService = paperService;
    }

    private Paper paper;
    @Resource
    private PaperService PaperService;
    private Pagination pagination;
    private Pagination paginationjson;
    private int currentpage;
    private int id;
    private int paperId;
    private List<?> datalist;
    private List clazzList;
    private Set dataset = new HashSet();
    private String password;
    private String index;

    private String status;
    private String selectOne;
    private String teacherOrexam;
    private String isjava;

    public String getPapers() {

        if (index == null) {
            index = "1";
        }
        if (selectOne == null) {
            selectOne = "";
        }
        if (teacherOrexam == null) {
            teacherOrexam = "";
        }
        if (status == null) {
            status = "";
        }
        if (isjava == null) {
            isjava = "";
        }

        int Iindex = Integer.parseInt(index);

        QueryTerm query = new QueryTerm(Iindex, selectOne, teacherOrexam, status, isjava);
        pagination = PaperService.queryForPage(query);

        return SUCCESS;
    }

    public void checkpw() throws IOException {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        out = response.getWriter(); // out.print("1%该用户还没注册");
        boolean flag = false;
        flag = PaperService.checkpw(password, id);
        if (flag) {
            out.print("yes");
        } else {
            out.print("no");
        }

    }

    public String showPaperScore() {

        User u = (User) session.get("loginUser");
        datalist = PaperService.showPaperScore(u.getId());
        return "showscore";
    }

    public String paperDel() {
        PaperService.paperDel(id);
        return "delok";
    }

    public String paperEdit() {
        paper = PaperService.findById(paperId);
        return "paperEdit";
    }

    public String PaperEditInfo() {
    	if(paper.getPaperpw().equals("")){
    		paper.setPaperpw(null);
    	}
        PaperService.PaperEditInfo(paper);
        return "paperEdit";
    }

    public String paperClazzEdit() {
        clazzList = PaperService.ClazzList();
        paper = PaperService.findById(paperId);
        Set set = paper.getClasses();
        for (Iterator itor = set.iterator(); itor.hasNext();) {
            Clazz c = (Clazz) itor.next();
            dataset.add(c.getId());
        }
        return "paperClazzEdit";
    }

}
